package cn.madis.ymcc.web.controller;

import cn.madis.ymcc.dto.CourseAddDto;
import cn.madis.ymcc.service.ICourseService;
import cn.madis.ymcc.domain.Course;
import cn.madis.ymcc.query.CourseQuery;
import cn.madis.ymcc.result.JSONResult;
import cn.madis.ymcc.result.PageList;
import cn.madis.ymcc.vo.Course2OrderMax;
import cn.madis.ymcc.vo.CourseInfoVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;


    /**
     *课程下架
     */
    @RequestMapping(value="/offLineCourse/{courseId}",method= RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("courseId") Long courseId){
        courseService.offLineCourse(courseId);
        return JSONResult.success();
    }

    /**
     *课程上架
     */
    @RequestMapping(value="/onLineCourse/{courseId}",method= RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('course:online')")
    public JSONResult onLineCourse(@PathVariable("courseId") Long courseId){
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult save(@RequestBody CourseAddDto courseAddDto){
        courseService.save(courseAddDto);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @PreAuthorize("hasAnyAuthority('course:list')")
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        EntityWrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }


    /**
     * 课程详情页数据
     */
    @RequestMapping(value = "/detail/data/{courseId}",method = RequestMethod.GET)
    public JSONResult detail(@PathVariable("courseId") Long courseId){
        CourseInfoVo detail = courseService.detail(courseId);
        return JSONResult.success(detail);
    }


    /**
     * 订单数据查询展示
     */
    @RequestMapping(value = "/info/{courseId}",method = RequestMethod.GET)
    public JSONResult info(@PathVariable("courseId") String courseId){
        Course2OrderMax course2OrderMax = courseService.info(courseId);
        return JSONResult.success(course2OrderMax);
    }
}
